//
//  ViewController.m
//  SizeToFit
//
//  Created by wangrui on 14-10-22.
//  Copyright (c) 2014年 PPS. All rights reserved.
//

#import "ViewController.h"
#import "UIView+VisiableBounds.h"

@interface ViewController ()

@end

@implementation ViewController


- (void)testLabel {
	UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, 200,100)];
	label.numberOfLines = 2; //改成单行,双行,多行查看效果
	[label showBounds];
	label.text = @"中华人民共和国";
	[self.view addSubview:label];
	
	[label sizeToFit];//1
	
	
	NSLog(@"newframe:%@",NSStringFromCGRect(label.frame));
	
	label.text = @"中华人民共和国------------------xxxxxxxxxxxxxxxxxxxxxx";
	CGSize size1 = [label sizeThatFits:label.bounds.size];
	CGSize size2 = [label sizeThatFits:CGSizeMake(300, 100)]; //此时的size相对于一个constrait rect
	CGSize size3 = [label sizeThatFits:CGSizeMake(600, 100)];
	NSLog(@"%@,%@,%@",NSStringFromCGSize(size1) ,NSStringFromCGSize(size2),NSStringFromCGSize(size3));
	
	//[label sizeToFit];//2：等价于传入size1
	
	CGRect rect = label.frame;
	rect.size = size1; //分别传入size1,size2,size3查看效果
	[label setFrame:rect];
	
}

- (void)testButton {
	UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
	[bt showBounds];
	[bt setFrame:CGRectMake(0, 200, 200, 100)];
	
	/**
	 *  setImage 是和title并成一排的，sizeThatFits算出来的值为默认width相加,height取大的
	 */
	//[bt setImage:[UIImage imageNamed:@"test.png"] forState:UIControlStateNormal];//注释查看结果
	
	
	/**
	 *  setBackgroundImage 和title是上下并存的，sizeThatFits算出来的值为默认width取大,height取大的
	 */
	[bt setBackgroundImage:[UIImage imageNamed:@"test.png"] forState:UIControlStateNormal];
	
	[bt setTitle:@"中华人民共和国" forState:UIControlStateNormal]; //注释查看结果
	[self.view addSubview:bt];
	
	CGSize size = [bt sizeThatFits:bt.bounds.size];
	NSLog(@"%@",NSStringFromCGRect(bt.bounds));
	NSLog(@"%@",NSStringFromCGSize(size));
}

- (void)testTextfied {
	
	UITextField *fied = [[UITextField alloc] initWithFrame:CGRectMake(0, 20, 100, 200)];
	[fied showBounds];
	[self.view addSubview:fied];
	
	fied.text = @"中华人民共和国------------------xxxxxxxxxxxxxxxxxxxxxxXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
	[fied sizeToFit]; //和UILabel 类似
}

- (void)viewDidLoad {
	[super viewDidLoad];
	
	[self testTextfied];
}

- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning];
	
}



@end
